/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package assignment.servlet;

import assignment.jaxb.products.ProductType;
import assignment.jaxb.products.Products;
import java.io.IOException;
import java.math.BigInteger;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

/**
 *
 * @author HoangVu
 */
public class MarshallProductToJavScript extends HttpServlet {


    private Products products = new Products();

    String driver="com.microsoft.sqlserver.jdbc.SQLServerDriver";
    String url="jdbc:sqlserver://localhost:1433;instanceName=SQLEXPRESS;databaseName=ShoppingDB";
     public Connection getConnection() {
         Connection con = null;
        try {

            Class.forName(driver);
            con = DriverManager.getConnection(url, "sa", "sa");

        } catch (SQLException ex) {
            Logger.getLogger(MarshallProductToJavScript.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(MarshallProductToJavScript.class.getName()).log(Level.SEVERE, null, ex);
        } 
        return con;
    }

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/xml");
        
        try {
            Connection con = getConnection();
            String sql = "SELECT * FROM Product WHERE isactive = 1";
            Statement stm = con.createStatement();
            ResultSet rs = stm.executeQuery(sql);

             while (rs.next()) {
                ProductType product = new ProductType();
                int id = rs.getInt("id");
                product.setId(new BigInteger(String.valueOf(id)));
                product.setProductName(rs.getString("productname"));
                float price = rs.getFloat("price");
                product.setPrice(price);
                product.setQuantity(new BigInteger(rs.getString("quantity")));
                product.setImageThumbnail(rs.getString("imageThumbnail"));
                int categoryid = rs.getInt("categoryid");
                product.setCategoryid(new BigInteger(String.valueOf(categoryid)));
                products.getProduct().add(product);
            }
             marshallXMLProduct(products,response.getOutputStream());

        } catch (SQLException ex) {
            Logger.getLogger(MarshallProductToJavScript.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void marshallXMLProduct(Products product, ServletOutputStream outputStream)
    {
         JAXBContext jaxb;
        try {
            jaxb = JAXBContext.newInstance(Products.class);
            Marshaller marshal;
            marshal = jaxb.createMarshaller();
            marshal.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,true);
            marshal.setProperty(Marshaller.JAXB_ENCODING,"UTF-8");
            marshal.marshal(product,outputStream);
        } catch (JAXBException ex) {
            Logger.getLogger(MarshallProductToJavScript.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
